package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuDao menuDao;

    //查询所有一级Menu
    @Override
    public List<Menu> findAll() {
        return menuDao.selectMenuLevelNWithoutChildren(1);
    }

    @Override
    public Menu selectMenuByIdWithoutChildren(int id) {
        return menuDao.selectMenuByIdWithoutChildren(id);
    }

    //分页查询一级Menu
    @Override
    public PageInfo<Menu> findAllWithoutChildrenByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Menu> menus = menuDao.selectMenuLevelNWithoutChildren(1);
        PageInfo<Menu> menuPageInfo = new PageInfo<>(menus);
        return menuPageInfo;
    }


    @Override
    public List<Menu> findAllBaseUserNameWithChildren(String username) {
        return menuDao.selectMenuLevel1BaseUserNameWithChildren(username);
    }


    @Override
    public List<Menu> findAllBaseUserNameWithoutChildren(String username) {
        return menuDao.selectMenuLevel1BaseUserNameWithoutChildren(username);
    }


    @Override
    public List<Menu> queryChildren(int menuId) {
        Menu menu = menuDao.selectMenuByIdWithChildren(menuId);
        return menu.getChildren();
    }


    // 条件分页查询
    @Override
    public PageInfo queryByKeyWord(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Menu> menus = null;
        if (queryPageBean.getQueryString() == null || queryPageBean.getQueryString().equals("")||queryPageBean.getQueryString().equals("null")) {
            menus = menuDao.selectMenuLevelNWithoutChildren(1);
        } else {
            menus = menuDao.queryByKeyWord(queryPageBean.getQueryString());
        }
        PageInfo<Menu> menuPageInfo = new PageInfo<>(menus);
        return menuPageInfo;
    }



    @Override
    public int selectLevel1MenuId() {
        return menuDao.selectLevel1MenuId();
    }

    @Override
    public List<Menu> findMenuByLevel(int level) {
        List<Menu> menus = menuDao.selectMenuLevelNWithoutChildren(level);
        return menus;
    }

    @Transactional
    @Override
    public void deleteByMenuIdCascade(int id) {
        Menu menu = menuDao.selectMenuByIdWithChildren(id);
        //删除子级
        menu.getChildren().stream().forEach(children -> {
            menuDao.deleteRoleMenuByMenuId(children.getId());
            menuDao.deleteMenuByParentMenuId(children.getId());
            menuDao.deleteMenuById(children.getId());
        });
        //删除父级自己
        menuDao.deleteRoleMenuByMenuId(id);
        menuDao.deleteMenuByParentMenuId(id);
        menuDao.deleteMenuById(id);
    }

    @Transactional
    @Override
    public void deleteByMenuIdSafe(int id) {
        Menu menu = menuDao.selectMenuByIdWithChildren(id);
        List<Menu> children = menu.getChildren();
        if (!(children == null || children.size()== 0) ){
            throw new RuntimeException("具有子级,不允许删除！");
        }
        Set<Role> roles = menu.getRoles();
        if (roles != null && roles.size() != 0) {
            throw new RuntimeException("具有关联的权限，不允许删除！");
        }
        //删除父级自己
        menuDao.deleteMenuById(id);
    }


    @Transactional
    @Override
    public void createMenu(Menu menu) {
        menuDao.insertMenu(menu);
        Integer id = menu.getId();
        menuDao.insertIntoRoleMenu(id);
    }

    @Transactional
    @Override
    public void updateMenu(Menu menu) {
        menuDao.updateMenu(menu);
    }


}
